package com.icss.train_sys_back.controller.admin.train_manage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.icss.train_sys_back.entity.train_manage.RailwayBureauEntity;
import com.icss.train_sys_back.entity.ResponseResult;
import com.icss.train_sys_back.entity.ResultEnum;
import com.icss.train_sys_back.service.train_manage.RailwayBureauManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 小泽
 * @createTime 2025/9/22 10:15
 */
@RestController
@RequestMapping("/admin/railway_bureau_manage")
@Slf4j
public class RailwayBureauManageController {
    @Autowired
    private RailwayBureauManageService trainManageService;

    //铁路局列表查询
    @GetMapping("/getList")
    public ResponseResult<PageInfo<RailwayBureauEntity>> getBureauList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String bureauName,
            @RequestParam(required = false) String bureauCode) {
        log.info("铁路局列表分页+条件检索： page={},pageSize={},bureauName={},bureauCode={}", page, pageSize, bureauName, bureauCode);

        PageHelper.startPage(page, pageSize);
        List<RailwayBureauEntity> bureauList = trainManageService.findBureauList(bureauName, bureauCode);

        PageInfo<RailwayBureauEntity> pageInfo = new PageInfo<>(bureauList);

        return ResponseResult.success(pageInfo);
    }

    //保存/更新铁路局
    @PostMapping("/save")
    public ResponseResult save(@RequestBody RailwayBureauEntity railwayBureau) {
        Integer bureauId = railwayBureau.getBureauId();
        //插入铁路局
        if (bureauId == null) {
            log.info("保存铁路局={}", railwayBureau);
            boolean res = trainManageService.save(railwayBureau);
            return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
        } else {
            //更新铁路局
            log.info("更新铁路局={}", railwayBureau);
            boolean res = trainManageService.update(railwayBureau);
            return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
        }
    }

    //根据id删除铁路局
    @DeleteMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") Integer id) {
        log.info("删除id = {} 的铁路局", id);
        boolean res = trainManageService.delete(id);
        return res ? ResponseResult.success() : ResponseResult.fail(ResultEnum.FAIL);
    }
    //2222

}
